package web.servlet;

import com.alibaba.fastjson.JSONObject;
import com.rdc.example.bean.PageBean;
import com.rdc.example.bean.Result;
import com.rdc.example.bean.dto.NewsDTO;
import com.rdc.example.bean.dto.SongDTO;
import com.rdc.example.bean.dto.UserDTO;
import com.rdc.example.service.NewsService;
import com.rdc.example.service.impl.NewsServiceImpl;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


@WebServlet("/news/*")
public class NewsServlet extends BaseServlet {

    NewsService newsService = new NewsServiceImpl();

    public void getNews(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        System.out.println("getNews...");
        //接收 当前页码 和 每页展示条数
        Integer currentPage = Integer.parseInt(req.getParameter("currentPage"));
        Integer pageSize = Integer.parseInt(req.getParameter("pageSize"));
        //在session中获取userId
        HttpSession session = req.getSession();
        UserDTO userDTO=((UserDTO) session.getAttribute("userDTO"));
        Integer userId=userDTO.getId();
        PageBean<NewsDTO> pageBean= newsService.getNewsListByReceiverId(userId,currentPage,pageSize);
        //json发送
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(JSONObject.toJSONString(pageBean));
    }

    public void updateIsRead(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        System.out.println("updateIsRead...");
        req.setCharacterEncoding("UTF-8");
        Integer songId=Integer.parseInt(req.getParameter("id"));
        Result result=newsService.updateIsRead(songId);
        //json发送
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(JSONObject.toJSONString(result));
    }


}